﻿using System;
using System.Collections.Generic;
using System.Text;

namespace mssql_password
{
    public class BinReader
    {
        internal string source { get; }
        internal bool PASS { get; }
        internal int pos { get; set; }
        public BinReader(string hexstring) { 
            source = hexstring;
            PASS = hexstring.StartsWith("0x");
            pos = 0;
            this.Length = (source.Length - 2 ) / 2;
        }

        public  int Length { get; }
        public byte ReadByte()
        {
            return byte.Parse(ReadHexString(1), System.Globalization.NumberStyles.HexNumber);
        }
        public byte[] Read(int length)
        {
            var bytes = new byte[length];
            var now = 0;

            while (now < length)
            {
                bytes[now++] = ReadByte();
            }
            return bytes;
        }

        public string ReadHexString(int bytes_length)
        {
            var hexstring = string.Empty;
            var len = (bytes_length << 1);
            hexstring = source.Substring(pos + 2, len);
            pos += len;
            return hexstring;
        }

        public static byte[] ReadHex(string hexstring)
        {
            var reader = new BinReader(hexstring);
            return reader.Read(reader.Length);
        }
    }
}
